// pages/my/changePhone/index.js
const app = getApp();
var that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    oldPhone: null,
    newPhone: null,
    code: null,
    hidden: true,
    count:60,  
    countConst:60, 
    isDisabled:false, // 按钮是否禁用
    title:"发送验证码", // 按钮显示文字
    titleConst:"发送验证码"
  },
    countdown: function(){
    let that = this;
    let count = this.data.count;
    // 当count不为0开始倒计时，当count为0就关闭倒计时
    // 定时器
    var countdown = setInterval(function(){
      if( count == 0 ){
        that.setData({
          title:that.data.titleConst,
          count: that.data.countConst,
          isDisabled:false
        });
        // 取消定时器
        clearInterval(countdown);
      } else {
        that.setData({
          title:count-- + "s后重试",
          isDisabled:true
        });
      }
    }, 1000);
  },
  //获取用户输入的新号码
  userNewPhone: function(e) {
    this.setData({
      newPhone: e.detail.value
    })
  },
  //获取用户输入的验证码
  loginBtnClick: function(e) {
    console.log(e.detail)
    this.setData({
      code: e.detail.value
    })
  },
  //发送验证码
  sendCheck() {
    let that = this
    // var token = "b0aacc03-6f7d-4574-8e5b-a20a7e6607e2";
    let token = app.globalData.token;
    let newPhone = this.data.newPhone;
    let oldPhone = app.globalData.user.phone;
    //var oldPhone = 11012341001;
    let userType = app.globalData.userType;
    //var userType = "admin";
    var phoneRepExp = /^1[0-9]{10}$/;
    //验证手机号
    if (newPhone == "" || newPhone == null) {
      wx.showToast({
        title: '请填写手机号',
        icon: 'none',
      });
      return false;
    } else if (!phoneRepExp.test(newPhone) || newPhone == "" || newPhone == null) {
      wx.showToast({
        title: '手机号错误',
        icon: 'none',
      });
      return false;
    }
    if (app.globalData.userType === "admin") {
      var url = app.globalData.BASE_URL + '/admin/adminUpdatePhoneCode'
    } else if (app.globalData.userType === "student1" || app.globalData.userType === "student2") {
      var url = app.globalData.BASE_URL + '/student/studentUpdatePhoneCodeServlet'
    } else if (app.globalData.userType === 'worker') {
      var url = app.globalData.BASE_URL + '/worker/workerUpdatePhoneCodeServlet'
    }
    wx.request({
      url: url,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {
        token: token,
        userType: userType,
        newPhone: newPhone,
        phone: oldPhone
      },
      success(res) {
        console.log(res)
        if (res.data == 1) {
        that.countdown();
          wx.showToast({
            title: '发送成功',
            icon: 'none',
          });
        } else if (res.data == 2 || res.data == 3) {
          wx.showToast({
            title: '发送失败！',
            icon: 'none',
          });
        } else if (res.data == 444) {
          wx.navigateTo({
            url: "/pages/login/index?flag=invalid",
          })
        } else {
          wx.showToast({
            title: '未知错误',
            icon: 'none',
          });
        }
      },
      fail(res) {
        that.setData({
          hiddenCode: true
        })
        wx.showToast({
          title: '请求超时！',
          icon: 'none',
        });
      }
    })
  },
  checkInfo: function() {

    let code = this.data.code
    let phone = this.data.newPhone
    console.log(code)
    console.log(phone)
    var phoneRepExp = /^1[0-9]{10}$/;
    var patt = /^\d{6}$/
    //验证手机号
    if (phone == "" || phone == null) {
      wx.showToast({
        title: '请填写手机号',
        icon: 'none',
      });
      return false;
    } else if (!phoneRepExp.test(phone) || phone == "" || phone == null) {
      wx.showToast({
        title: '手机号错误',
        icon: 'none',
      });
      return false;
    }
    //验证验证码
    else if (!patt.test(code) || code == "" || code == null) {
      wx.showToast({
        title: '请填写6位数字验证码',
        icon: 'none',
      });
      return false;
    }

    return true;
  },
  submit() {
    let code = this.data.code
    let phone = this.data.newPhone
    if (!this.checkInfo()) {
      return false;
    }
    that.setData({
      hidden: false
    })
    console.log(this.data.code)
    if (app.globalData.userType === "admin") {
      var url = app.globalData.BASE_URL + '/admin/adminUpdatePhone'
    } else if (app.globalData.userType === "student1" || app.globalData.userType === "student2") {
      var url = app.globalData.BASE_URL + '/student/studentUpdatePhoneServlet'
    } else if (app.globalData.userType === 'worker') {
      var url = app.globalData.BASE_URL + '/worker/workerUpdatePhoneServlet'
    }
    wx.request({
      url: url,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {
        token: app.globalData.token,
        userType: app.globalData.userType,
        id: app.globalData.user.id,
        phone: app.globalData.user.phone,
        newPhone: phone,
        code: code
      },
      success(res) {
        that.setData({
          hidden: true
        })
        console.log(res)
        if (res.data == 1) {
          wx.showToast({
              title: '修改成功',
              icon: 'success',
              duration: 1500
            }),
            setTimeout(() => {
              wx.navigateTo({
                url: "/pages/login/index?flag=invalid",
              })
            }, 1500)
        } else if (res.data == 2) {
          wx.showToast({
            title: '修改失败！',
            icon: 'none',
          });
        } else if (res.data == 3) {
          wx.showToast({
            title: '验证码错误！',
            icon: 'none',
          });
        } else if (res.data == 444) {
          wx.navigateTo({
            url: "/pages/login/index?flag=invalid",
          })
        }
      },
      fail(res) {
        that.setData({
          hidden: true
        })
        wx.showToast({
          title: '请求超时！',
          icon: 'none',
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})